package org.iteye.bbjava.util.interpreter;

import java.util.List;

import org.dom4j.Element;
import org.dom4j.Node;

public class TerminalExpression extends AbstractExpression {

	Context ctx;

	public TerminalExpression(Context ctx){
		this.ctx = ctx;
	}
	
	public TerminalExpression( ){
	}
	
	
	public void setContext(Context ctx) {
		this.ctx = ctx;
	}


	@Override
	public String[] interpret() {
		if(null == this.ctx )
			return null;
		
		List<Node> nodes = ctx.queryNodes("//attribute");
		StringBuffer sb = new StringBuffer();
		String javaType = null;
		String property = null;
		
		for(Node node:nodes){
			Element elem = (Element)node;
			javaType =  elem.attribute("javaType").getText();
			property = elem.attribute("name").getText() ;
			sb.append("public " + javaType + " " + property + ";,");
			sb.append("public void set" + property +"("+javaType  + " " + property + "){this."+property+" ="+property+"};,");
			sb.append("public " + javaType + " get"+property + "();,");
		}
		String str = sb.toString();
		String [] properties = str.split(",");
		return properties;
	}
	
	public String createFinalResult(String type){
		String[] strs= this.interpret();
		StringBuffer sb = new StringBuffer();
		for(int i=0 ; i <= strs.length-1 ; i++){
			sb.append(strs[i]+" 	");	
		}
		return sb.toString();
	}

}
